from feature_data.const_data import *

# title1,  title2, common word  set
def get_common_word_set(title1, title2):

    token_list1 = title1.split(' ')
    token_list2 = title2.split(' ')

    common_word_set = set(token_list1).intersection(set(token_list2) )
    return common_word_set


# title1,  title2, common str set
def get_common_str_set(title1, title2):
    common_word_set = get_common_word_set(title1, title2)
    all_common_str = []
    for i in range( len(common_word_set)):
        for j in range( len(common_word_set) ):
            if i != j:
                common_str = list(common_word_set)[i] + ' ' + list(common_word_set)[j]
                all_common_str.append( common_str )
    common_str_list = set()
    for common_str in all_common_str:
        if common_str in title1 and common_str in title2:
            common_str_list.add( common_str )
    return common_str_list

# title1,  title2, bool
def have_common_str_set(title1, title2):
    common_str_list = get_common_str_set(title1, title2)

    if len( common_str_list ) != 0:
        return True, common_str_list
    else:
        return False, None


# clust_title_list  clust_common_str_set  common_str_idx_dict
def get_cluster_common_str_set_idx_dict(paper_idx_list, clust_title_list):
    clust_common_str_set = set()
    common_str_idx_dict = {}
    for i in range(len(clust_title_list) - 1):
        for j in range(i + 1, len(clust_title_list)):
            flag, common_str_list = have_common_str_set(clust_title_list[i], clust_title_list[j])
            if flag:
                for common_str in common_str_list:
                    clust_common_str_set.add(common_str)
                    if common_str not in common_str_idx_dict:
                        common_str_idx_dict[common_str] = set([ paper_idx_list[i], paper_idx_list[j] ])
                    else:
                        common_str_idx_dict[common_str].add(paper_idx_list[i])
                        common_str_idx_dict[common_str].add(paper_idx_list[j])

    return clust_common_str_set , common_str_idx_dict
"""
def have_venue_word_set( venue1 ):
    venue_word_list = venue1.split(' ')

    if len( venue_word_list ) > 1:
        return True, venue_word_list
    else:
        return False, None

def get_clust_all_venue_word_set_idx_dict(paper_idx_list, clust_venue_list):

    clust_all_venue_word_set = set()
    venue_word_idx_dict = {}
    # for i in range(len(clust_venue_list) - 1):
    for i in range(len(clust_venue_list)):
        flag, venue_word_list = have_venue_word_set(clust_venue_list[i])
        if flag:
            for venue_word in venue_word_list:
                clust_all_venue_word_set.add(venue_word)
                if venue_word not in venue_word_idx_dict:
                    venue_word_idx_dict[venue_word] = [ paper_idx_list[i] ]
                else:
                    venue_word_idx_dict[venue_word].append(paper_idx_list[i])

    return clust_all_venue_word_set, venue_word_idx_dict
"""

"""
def have_venue_word_set( venue1 ):
    venue_word_list = venue1.split(' ')

    if len( venue_word_list ) > 1:
        return True, venue_word_list
    else:
        return False, None

def get_clust_all_venue_word_set_idx_dict(paper_idx_list, clust_venue_list):

    venue_word_set_list = []
    venue_word_idx_dict = {}
    # for i in range(len(clust_venue_list) - 1):
    for i in range(len(clust_venue_list)):
        flag, venue_word_list = have_venue_word_set(clust_venue_list[i])
        if flag:
            venue_word_set_list.append(set(venue_word_list))
            for venue_word in venue_word_list:
                if venue_word not in venue_word_idx_dict:
                    venue_word_idx_dict[venue_word] = [ paper_idx_list[i] ]
                else:
                    venue_word_idx_dict[venue_word].append(paper_idx_list[i])
     #   you can modify, must have two, you can intersection, one has possibility

    if len(venue_word_set_list) > 1:
        result_set = venue_word_set_list[0]
        for venue_word_set in venue_word_set_list:
            result_set = result_set.intersection(venue_word_set)

        return result_set, venue_word_idx_dict
    else:
        return None, None
"""
def have_venue_word_set( venue1 ):
    venue_word_list = venue1.split(' ')

    if len( venue_word_list ) > 1:
        return True, venue_word_list
    else:
        return False, None

def get_clust_all_venue_word_set_idx_dict(paper_idx_list, clust_venue_list):

    venue_word_frequency_dict = {}
    venue_word_idx_dict = {}
    # for i in range(len(clust_venue_list) - 1):
    for i in range(len(clust_venue_list)):
        flag, venue_word_list = have_venue_word_set(clust_venue_list[i])
        if flag:
            for venue_word in venue_word_list:
                if venue_word not in venue_word_idx_dict:
                    venue_word_idx_dict[venue_word] = [ paper_idx_list[i] ]
                else:
                    venue_word_idx_dict[venue_word].append(paper_idx_list[i])

                if venue_word not in venue_word_frequency_dict:
                    venue_word_frequency_dict[venue_word] = 1
                else:
                    venue_word_frequency_dict[venue_word] += 1


    if len(venue_word_frequency_dict) > 1:
        """
           you can refactor into a inverse function
        """
        venue_frequency_word_dict = {}
        for venue_word, frequecy in venue_word_frequency_dict.items():
            if frequecy not in venue_frequency_word_dict:
                venue_frequency_word_dict[frequecy] = [venue_word]
            else:
                venue_frequency_word_dict[frequecy].append(venue_word)

        max_freqency = sorted(venue_frequency_word_dict.keys(), reverse=True)[0]

        venue_word_set = set(venue_frequency_word_dict[max_freqency])
        return venue_word_set, venue_word_idx_dict
    else:
        return None, None


# university is first
'''
this is processing organization, 'null' is no effect.
'''
def get_org_str(org):
    org_str_list = None
    try:
        org_str_list = org.split(',')

        for keyword in keywords:
            for org_str in org_str_list:
                if keyword in org_str:
                    return org_str.strip()
    except:
        print org_str_list
    return None


def get_cluster_common_org_idx(paper_idx_list, clust_org_list):
    org_str_idx_list_dict = {}
    for i in range( len(clust_org_list) ):
        org_str = get_org_str(clust_org_list[i])
        if org_str != None:
            if org_str not in org_str_idx_list_dict:
                org_str_idx_list_dict[org_str] = [ paper_idx_list[i] ]
            else:
                org_str_idx_list_dict[org_str].append(paper_idx_list[i]  )
    if len(org_str_idx_list_dict) != 0:
        sorted(org_str_idx_list_dict.items(), key=lambda d: len( d[1] ) ,reverse=True)
        return org_str_idx_list_dict.items()[0][0], org_str_idx_list_dict.items()[0][1]
    else:
        return None, -1
# one title  cluster_title_list  intersection title
# for  have_common_str_set

# one title cluster_title_list  intersection common_str  common_str_idx


# 3
# clust_title_list1  clust_title_list2  clust_common_str_set have intersection common_str_idx 1, 2



# one_idx_clust_title_list   clust result by title common str


# for every one_idx_title 1
# one_idx_title  clust_title_list  if have  then return clust result


# for every one_idx_title 2
# one_idx_title cluster_title_list if have common_str return clust result common_str_idx


# idx_list
# title_list


# one_idx_list

# many_idx_list


# add_edge

#  pair_set_graph


# util class and service class  danyi_zhize
# para is little lidu





